package io.renren.modules.skate.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.skate.dto.FCertificateDTO;
import io.renren.modules.skate.dto.FClassifyDTO;
import io.renren.modules.skate.dto.FCompetitionDTO;
import io.renren.modules.skate.dto.FContestItemDTO;
import io.renren.modules.skate.excel.FAthleteExcel;
import io.renren.modules.skate.excel.FCertificateExcel;
import io.renren.modules.skate.excel.FGradeCertificateExcel;
import io.renren.modules.skate.listener.FCertificateListener;
import io.renren.modules.skate.listener.FGradeCertificateListener;
import io.renren.modules.skate.service.FCertificateService;
import io.renren.modules.skate.service.FClassifyService;
import io.renren.modules.skate.service.FCompetitionService;
import io.renren.modules.skate.service.FContestItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import liquibase.pro.packaged.A;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
* 证书表
*
* @author Mark sunlightcs@gmail.com
* @since 3.0 2023-11-28
*/
@RestController
@RequestMapping("skate/fcertificate")
@Api(tags="证书表")
public class FCertificateController {
    @Autowired
    private FCertificateService fCertificateService;

    @Autowired
    private FCompetitionService fCompetitionService;
    @Autowired
    private FContestItemService fContestItemService;
    @Autowired
    private FClassifyService fClassifyService;

    @Autowired
    private FGradeCertificateListener fGradeCertificateListener;

    @Autowired
    private FCertificateListener fCertificateListener;


    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("skate:fcertificate:page")
    public Result<PageData<FCertificateDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<FCertificateDTO> page = fCertificateService.page(params);

        page.getList().forEach( dto ->{
           /* if(dto.getCompetitionId() != null){
                FCompetitionDTO fCompetitionDTO = fCompetitionService.get(dto.getCompetitionId().longValue());
                dto.setCompetitionName(fCompetitionDTO.getName());
            }
            if(dto.getContestItemId() != null){
                FContestItemDTO fContestItemDTO = fContestItemService.get(dto.getContestItemId().longValue());
                dto.setContestItemName(fContestItemDTO.getItemName());
            }*/
            if(dto.getClassifyId() != null){
                FClassifyDTO fClassifyDTO = fClassifyService.get(dto.getClassifyId().longValue());
                dto.setClassifyName(fClassifyDTO.getClassifyName());
            }

        });


        return new Result<PageData<FCertificateDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("skate:fcertificate:info")
    public Result<FCertificateDTO> get(@PathVariable("id") Long id){
        FCertificateDTO data = fCertificateService.get(id);

        return new Result<FCertificateDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("skate:fcertificate:save")
    public Result save(@RequestBody FCertificateDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setCreateDate(new Date());
        dto.setCreator(SecurityUser.getUserId().toString());
        dto.setUpdater(SecurityUser.getUserId().toString());
        dto.setUpdateDate(new Date());
        fCertificateService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("skate:fcertificate:update")
    public Result update(@RequestBody FCertificateDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        dto.setUpdater(SecurityUser.getUserId().toString());
        dto.setUpdateDate(new Date());
        fCertificateService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("skate:fcertificate:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        fCertificateService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("skate:fcertificate:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<FCertificateDTO> list = fCertificateService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "证书表", list, FCertificateExcel.class);
    }

    /**
     * 导入考级人员
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("gradeImport")
    @ApiOperation("导入")
    @ApiImplicitParam(name = "file", value = "文件", paramType = "query", dataType="file")
    public Result gradeImport(@RequestParam("file") MultipartFile file) throws Exception {

        ExcelReaderBuilder readCertificate = EasyExcel.read(file.getInputStream(), FGradeCertificateExcel.class, fGradeCertificateListener);

        ExcelReaderSheetBuilder sheet = readCertificate.sheet();

        sheet.doRead();

        return new Result();
    }

    /**
     * 导入参赛人员
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("matchImport")
    @ApiOperation("导入")
    @ApiImplicitParam(name = "file", value = "文件", paramType = "query", dataType="file")
    public Result matchImport(@RequestParam("file") MultipartFile file) throws Exception {

        ExcelReaderBuilder readCertificate = EasyExcel.read(file.getInputStream(), FCertificateExcel.class, fCertificateListener);

        ExcelReaderSheetBuilder sheet = readCertificate.sheet();

        sheet.doRead();

        return new Result();
    }

}